<?php

/**
 * sistema actions.
 *
 * @package    IBE
 * @subpackage sistema
 * @author     Creatix
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sistemaActions extends sfActionsCreatix
{
	public function executeIndex(sfWebRequest $request)
	{
		return $this->redirect($this->getModuleName().'/edit?id_sistema=1');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($sistema = Doctrine::getTable('Sistema')->find(array($request->getParameter('id_sistema'))), sprintf('Object sistema does not exist (%s).', $request->getParameter('id_sistema')));
		$this->form = new SistemaForm($sistema);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($sistema = Doctrine::getTable('Sistema')->find(array($request->getParameter('id_sistema'))), sprintf('Object sistema does not exist (%s).', $request->getParameter('id_sistema')));
		$this->form = new SistemaForm($sistema);

		$this->processForm($request, $this->form);

		$this->forward($this->getModuleName(), 'edit');
	}

	public function validateUpdate()
	{
		return true;
	}

	public function handleErrorUpdate()
	{
		return $this->forward($this->getModuleName(), 'edit');
	}

	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		try
		{
			$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
			if ($form->isValid())
			{
				$sistema = $form->save();
				$this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
				$this->redirect('sistema/edit?id_sistema='.$sistema->getIdSistema());
			}
			else
			{
				throw new Exception($form->getErrorSchema());
			}
		}
		catch(Exception $e)
		{
			$this->getUser()->setFlash('erro', $e->getMessage());
		}
	}
}
